package com.teach.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.teach.commons.FileUtil;
import com.teach.commons.Id;
import com.teach.dto.PhotoFormDTO;
import com.teach.entity.Album;
import com.teach.entity.Photo;
import com.teach.exception.CodeException;
import com.teach.mapper.AlbumMapper;
import com.teach.mapper.PhotoMapper;
import com.teach.service.PhotoService;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * <pre>
 * 
 * 创建时间 ：	2019年12月4日 下午3:10:46
 * 当前版本：	V1.0
 * Copyright © 1998 - 2019 jlzhou.top. All Rights Reserved
 * </pre>
 * 
 * @author J.L.Zhou
 *
 */
@Service
@Slf4j
public class PhotoServiceImpl implements PhotoService {

	@Autowired
	private PhotoMapper mapper;

	@Autowired
	private AlbumMapper albumMapper;

	@Value("${root:d:/upload}")
	private String root;

	@Autowired
	private Id id;

	@Override
	@Transactional
	public void upload(PhotoFormDTO form, MultipartFile img) {
		// 创建要保存的相片的唯一路径
		Long ids = id.nextId();
		String uri = "photo/" + new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + ids + "/";
		File path = new File(root, uri);
		path.mkdirs();

		// 保存原始图片
		File file = new File(path, "1." + FileUtil.getFileExt(img.getOriginalFilename()));
		try {
			img.transferTo(file);
		} catch (Exception e) {
			throw new CodeException(100);
		}

		// 生成缩略图
		BufferedImage i = null;
		try {
			i = ImageIO.read(file);
			Thumbnails.of(i).size(100, 100).outputFormat("png").toFile(new File(path, "2.png"));
		} catch (Exception e) {
			throw new CodeException(101);
		}

		// 生成加水印大图
		try {
			BufferedImage image = ImageIO.read(new File(root, "_w.png"));
			Thumbnails.of(i).size(800, 800).watermark(Positions.CENTER, image, 1).outputFormat("png")
					.toFile(new File(path, "3.png"));
		} catch (Exception e) {
			throw new CodeException(102);
		}
		
		// 记录上传的相片信息
		Photo p = new Photo();
		BeanUtils.copyProperties(form, p);
		p.setPhotoId(ids);
		if (StringUtils.isEmpty(p.getPhotoName())) {
			p.setPhotoName(img.getOriginalFilename());
		}
		p.setPhotoSize(img.getSize());
		p.setPhotoUploadTime(new Date());
		p.setPhotoPath(uri);
		mapper.insertSelective(p);

		// 修改相册的相关信息
		Album a = albumMapper.selectByPrimaryKey(p.getAlbumId());
		a.setAlbumLastModifyTime(p.getPhotoUploadTime());
		a.setAlbumNum(a.getAlbumNum() + 1);
		a.setAlbumPhotoId(p.getPhotoId());
		albumMapper.updateByPrimaryKey(a);

	}
}
